/**
 * 
 */
package rs.fon.is.music.web.listeners;

import java.io.File;
import java.util.LinkedList;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import rs.fon.is.music.config.Settings;
import rs.fon.is.music.domain.MusicGroup;
import rs.fon.is.music.parser.LastFMParser;
import rs.fon.is.music.persistence.DataModelManager;

/**
 * @author "Nikola Milikic"
 * 
 */
public class InitializationListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent contextEvent) {
		ServletContext context = contextEvent.getServletContext();
		String pathPrefix = context.getRealPath("/WEB-INF");
		Settings.contextPath = pathPrefix;
		
		if (Settings.getInstance().config.performParsing) {
			File file = new File(Settings.getInstance().config.tdbFolderPath);
			if (file.exists()) {
				file.delete();
			}
			
			// invoke parser
			LastFMParser parser = new LastFMParser();
			try {
				parser.parse("http://www.last.fm/music");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		DataModelManager.getInstance().closeDataModel();
	}

}
